using Caching.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Caching.Controllers {
  public class HomeController : Controller {
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger) {
      _logger = logger;
    }

    public IActionResult Index() {
      return View();
    }

    public IActionResult Privacy() {
      return View();
    }



    /*
     In order to use OutputCahe in ASP.NET MVC Core, you need to do the following:
      1) Add the package  WebEssentials.AspNetCore.OutputCaching in Nuget
      2) Add services.AddOutputCaching(); in ConfigureServices(..) in startup
      3) Add app.UseOutputCaching(); in Configure(...) in startup

     https://stackoverflow.com/questions/70158569/saving-the-response-cache-to-the-server-in-net-5
     */
    [OutputCache(Duration=60, UseAbsoluteExpiration = true, VaryByParam ="x")]
    public IActionResult GetDateTime(int? x, int? y) {
      return Ok($"Responses are generated on {DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss")}");
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error() {
      return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
  }
}
/*
Criteria to determine cache or not cache
1) Content creation is costly
    - Performance
    - Resource Utilization
2) Content is durable
3) Access frequency is high
 */